package com.zmsoft.boxy.sfg.service;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.RandomUtils;
import org.cnsoft.framework.security.MD5SecurityHelper;
import org.cnsoft.framework.support.MyServiceSupport;
import org.cnsoft.framework.utils.EmptyHelper;

import com.zmsoft.boxy.sfg.cache.SFGGameInfoCacheService;
import com.zmsoft.boxy.sfg.cache.SFGPlayerInfoCacheService;
import com.zmsoft.boxy.sfg.player.BoxySFGPlayer;
import com.zmsoft.boxy.sfg.player.SFGGameConstants;
import com.zmsoft.boxy.sfg.player.SfgPlayerFriendDBO;
import com.zmsoft.persistent.sfg.Sfg661010PlayerFarm.Sfg661010PlayerFarmDBO;
import com.zmsoft.persistent.sfg.Sfg661021PlayerLand.Sfg661021PlayerLandDBO;
import com.zmsoft.persistent.sfg.Sfg661050PlayerFriend.Sfg661050PlayerFriendDBO;
import com.zmsoft.persistent.sfg.Sfg662040GameCrops.Sfg662040GameCropsDBO;
import com.zmsoft.persistent.sfg.Sfg662041GameCropsGrow.Sfg662041GameCropsGrowDBO;

/**
 * 游戏推进器（后台任务处理）<br>
 * 依靠被动刷新和前台定时器来确认成长进度
 * @author ZmSoft
 * @version 0.1.0 2018/3/1
 * @since 0.1.0 2018/3/1
 * @see ApiManagerLoginController
 */
//@Service("SFGGameThrusterService")
//@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class SFGGameThrusterServiceHelper extends MyServiceSupport implements SFGGameConstants {
	public static void preparePlayerFriend(BoxySFGPlayer player, SfgPlayerFriendDBO friend,Sfg661050PlayerFriendDBO playerFriend) throws Exception {
		//确认好友ID
		if (player.getId().compareTo(friend.getPlayerFriendId())>0) {
			playerFriend.setPlayerId1(friend.getPlayerFriendId());
			playerFriend.setPlayerId2(player.getId());
		} else {
			playerFriend.setPlayerId1(player.getId());
			playerFriend.setPlayerId2(friend.getPlayerFriendId());
		}
		
		playerFriend.setId(MD5SecurityHelper.encrypt(playerFriend.getPlayerId1()+playerFriend.getPlayerId2()));
	}
	
//	/**
//	 * 好友算法hash
//	 * @return
//	 * @throws Exception
//	 */
//	public String prepareFriendID() throws Exception {
//		// 比较friendId和UserId的大小,进行从大到小排序（新人+旧人）
//		// 如果参数字符串等于此字符串，则返回值 0；
//		if (EmptyHelper.isEmpty(this.getFriendId())) {
//			return EMPTY;
//		} else if (EmptyHelper.isEmpty(super.getUserId())) {
//			return EMPTY;
//		} else if (this.getFriendId().compareTo(super.getUserId()) == 0) {
//			// 代表着自己，不能操作
//			return EMPTY;
//		}
//		// 如果此字符串小于字符串参数，则返回一个小于 0 的值；
//		else if (this.getFriendId().compareTo(super.getUserId()) < 0) {
//			return MD5SecurityHelper.encrypt(super.getUserId() + this.getFriendId()).toLowerCase();
//		}
//		// 如果此字符串大于字符串参数，则返回一个大于 0 的值。
//		else if (this.getFriendId().compareTo(super.getUserId()) > 0) {
//			return MD5SecurityHelper.encrypt(this.getFriendId() + super.getUserId()).toLowerCase();
//		}
//		return EMPTY;
//	}
	
	// 好友模式刷新
	public static void preparePlayerLand(SFGGameInfoCacheService sfgGameInfoCacheService, SFGPlayerInfoCacheService sfgPlayerInfoCacheService,
			BoxySFGPlayer player) throws Exception {
		List<Sfg661021PlayerLandDBO> playerLands = sfgPlayerInfoCacheService.loadPlayerLands(player);
		for (Sfg661021PlayerLandDBO land : playerLands) {
			preparePlayerLand(sfgGameInfoCacheService, sfgPlayerInfoCacheService,player, land);
		}
	}

	// 玩家模式刷新
	// 1土地种子/2幼苗发芽/3茁壮成长/4长大开花/5成熟结果
	public static void preparePlayerLand(SFGGameInfoCacheService sfgGameInfoCacheService, SFGPlayerInfoCacheService sfgPlayerInfoCacheService,
			BoxySFGPlayer player, Sfg661021PlayerLandDBO playerLand) throws Exception {
		// 用户农场信息
		Sfg661010PlayerFarmDBO playerFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player);
		if (EmptyHelper.isEmpty(playerFarm) || EmptyHelper.isEmpty(playerLand)) {
			return;
		}

		// 系统时间
		Long currentTimeMillis = System.currentTimeMillis();
		// 种植时间-秒
		// Long plantTime = playerLand.getPlantTime();// 开始时间

		// 流失时间-秒
		Long lostTimeSecone = TimeUnit.MILLISECONDS.toSeconds(currentTimeMillis - playerLand.getPlantTime());
		// 成熟需要时间秒
		Long cropsNeedTime = playerLand.getCropsNeedTime();

		// 离线时间>成熟需要时间秒
		if (lostTimeSecone > cropsNeedTime) {
			// 直接成熟，计算果实
			Sfg662040GameCropsDBO gameCrop = sfgGameInfoCacheService.loadGameCropsData(SFG_GAME_FARM_TYPE, playerLand.getCropsId());
			// 当前阶段ID
			playerLand.setStageId(SIX);
			// 当前阶段名称
			playerLand.setStageName(SFG_Fini_StageName);

			// 当前阶段需要时间(秒) need_time
			playerLand.setNeedTime(0L);
			// 季数
			playerLand.setSeasonsNum(gameCrop.getMaxSeasonsNum());
			// 收获果实数目-最小值-最大值
			playerLand.setFruitMax(RandomUtils.nextLong(gameCrop.getFruitMin(), gameCrop.getFruitMax()));
			// 最大可偷取果实数目
			playerLand.setFruitStealMax((long) (playerLand.getFruitMax() * 0.1));
			// 剩余可偷取果实数目
			playerLand.setFruitStealLeft(playerLand.getFruitStealMax());
			// 果实单价-分
			playerLand.setFruitPrice(gameCrop.getFruitPrice());
			// 果实占用仓库容积大小
			playerLand.setFruitVolume(gameCrop.getFruitVolume());

			// 动作增加经验
			playerFarm.setTotalExp(playerFarm.getTotalExp() + gameCrop.getCropsExp());

			// 用户数据保存
			sfgPlayerInfoCacheService.savePlayerFarm(playerFarm);
			sfgPlayerInfoCacheService.savePlayerLand(player, playerLand);
		} else {
			// 离线时间<=成熟需要时间秒
			// 判断处于哪个阶段
			// 流失时间-秒
			lostTimeSecone = TimeUnit.MILLISECONDS.toSeconds(currentTimeMillis - playerLand.getCropsNeedTime1());// 1土地种子
			if (lostTimeSecone > 0) {
				Sfg662041GameCropsGrowDBO gameCropsGrowDBO = null;
				lostTimeSecone = TimeUnit.MILLISECONDS.toSeconds(currentTimeMillis - playerLand.getCropsNeedTime2());// 2幼苗发芽
				if (lostTimeSecone > 0) {
					lostTimeSecone = TimeUnit.MILLISECONDS.toSeconds(currentTimeMillis - playerLand.getCropsNeedTime2());// 3茁壮成长
					if (lostTimeSecone > 0) {
						lostTimeSecone = TimeUnit.MILLISECONDS.toSeconds(currentTimeMillis - playerLand.getCropsNeedTime2());// 4长大开花
						if (lostTimeSecone > 0) {
							lostTimeSecone = TimeUnit.MILLISECONDS.toSeconds(currentTimeMillis - playerLand.getCropsNeedTime2());// 5成熟结果
							if (lostTimeSecone > 0) {
								// 成熟阶段
							} else {
								gameCropsGrowDBO = sfgGameInfoCacheService.loadGameCropsGrowData(SFG_GAME_FARM_TYPE, playerLand.getCropsId(), FIVE);
								// 动作增加经验
								// playerFarm.setTotalExp(playerFarm.getTotalExp() + gameCrop.getCropsExp());
							}
						} else {
							gameCropsGrowDBO = sfgGameInfoCacheService.loadGameCropsGrowData(SFG_GAME_FARM_TYPE, playerLand.getCropsId(), FOUR);
							// 动作增加经验
							playerFarm.setTotalExp(playerFarm.getTotalExp() + gameCropsGrowDBO.getCropsExp());
							// 用户数据保存
							sfgPlayerInfoCacheService.savePlayerFarm(playerFarm);
						}
					} else {
						gameCropsGrowDBO = sfgGameInfoCacheService.loadGameCropsGrowData(SFG_GAME_FARM_TYPE, playerLand.getCropsId(), THREE);
						// 动作增加经验
						playerFarm.setTotalExp(playerFarm.getTotalExp() + gameCropsGrowDBO.getCropsExp());
						// 用户数据保存
						sfgPlayerInfoCacheService.savePlayerFarm(playerFarm);
					}
				} else {
					gameCropsGrowDBO = sfgGameInfoCacheService.loadGameCropsGrowData(SFG_GAME_FARM_TYPE, playerLand.getCropsId(), TWO);
					// 动作增加经验
					playerFarm.setTotalExp(playerFarm.getTotalExp() + gameCropsGrowDBO.getCropsExp());
					// 用户数据保存
					sfgPlayerInfoCacheService.savePlayerFarm(playerFarm);
				}

				//更新阶段数据
				if (EmptyHelper.isNotEmpty(gameCropsGrowDBO)) {
					// 当前阶段ID
					playerLand.setStageId(gameCropsGrowDBO.getId());
					// 当前阶段名称
					playerLand.setStageName(gameCropsGrowDBO.getStageName());
					// 土地图片
					playerLand.setLandPicUrl(gameCropsGrowDBO.getStagePicUrl());
					// 阶段互动ID
					playerLand.setActionId(gameCropsGrowDBO.getActionId());
					// 阶段互动名称
					playerLand.setActionName(gameCropsGrowDBO.getActionName());
					// 当前阶段需要时间(秒) need_time
					playerLand.setNeedTime(gameCropsGrowDBO.getNeedTime());
					// 剩余浇水数目
					playerLand.setLeftWatering(gameCropsGrowDBO.getLeftWatering());
					// 剩余除草数目
					playerLand.setLeftWeeding(gameCropsGrowDBO.getLeftWeeding());
					// 剩余杀虫数目
					playerLand.setLeftInsect(gameCropsGrowDBO.getLeftInsect());
					// 剩余施肥数目
					playerLand.setLeftFertilize(gameCropsGrowDBO.getLeftFertilize());
					// 可以获得经验数目
					playerLand.setCropsExp(gameCropsGrowDBO.getCropsExp());

					// 用户数据保存
					sfgPlayerInfoCacheService.savePlayerLand(player, playerLand);
				}
			}
		}

	}

}
